using System;
using System.Collections;
using System.Text;
using System.Text.RegularExpressions;

namespace ObjectRelationalMapping.Model
{
    internal class Mapping
    {
        internal Mapping ( string name )
        {
            this.name = name;
        }

        internal string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string name;
    }

    internal class MappingCollection : CollectionBase
    {
        internal Mapping this[ int index ]
        {
            get { return ( Mapping )( List[ index ] ); }
            set { List[ index ] = value; }
        }

        internal int Add ( Mapping mapping )
        {
            return List.Add ( mapping );
        }

        internal bool Contains ( string name )
        {
            bool result = false;

            string pattern = string.Empty;
            foreach ( Mapping mapping in this )
            {
                pattern = "(" + mapping.Name.Replace ( "*", string.Empty ) + ")";
                result = Regex.IsMatch ( name, pattern );
                if ( result )
                {
                    break;
                }
            }

            return result;
        }

        public override string ToString ( )
        {
            StringBuilder result = new StringBuilder ();

            for ( int index = 0; index <= ( this.Count - 1 ); ++index )
            {
                result.Append ( this[ index ].Name );
                if ( index < ( this.Count - 1 ) )
                {
                    result.Append ( ", " );
                }
            }

            return result.ToString ();
        }
    }
}